package com.animee.day11.slice;

import com.animee.day11.ResourceTable;
import com.animee.day11.utils.ImageSliderProvider;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;

import java.util.ArrayList;
import java.util.List;

public class PageSliderAbilitySlice extends AbilitySlice {
    PageSlider pageSlider;
    PageSliderIndicator indicator;
    // Text上显示的标题名
    //获取到的数据源，这个内容可以是集合，后面展示组件上
    String[]titles = {"新疆美女","迪丽热巴","长得好看","夏日沙滩"};
    int[]imgIds = {ResourceTable.Media_lunbo1,ResourceTable.Media_lunbo2,
            ResourceTable.Media_lunbo3,ResourceTable.Media_lunbo4};

    //每一个页面的组件
    List<Component>mDatas = new ArrayList<>();
    private ImageSliderProvider provider;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_pageslider);
        initView();
    }

    private void initView() {
        pageSlider = findComponentById(ResourceTable.Id_page_slider);
        indicator = findComponentById(ResourceTable.Id_page_indicator);

        initPoint();
        createAndshowPage();
    }
    //初始化小圆点=====设置指示器的样式
    private void initPoint() {
        ShapeElement normalShape = new ShapeElement();
        normalShape.setRgbColor(RgbColor.fromArgbInt(0x808a87));
        normalShape.setAlpha(160);
        normalShape.setShape(ShapeElement.OVAL);
        normalShape.setBounds(0,0,32,32);

        ShapeElement selectShape = new ShapeElement();
        selectShape.setRgbColor(RgbColor.fromArgbInt(0x872657));
        selectShape.setAlpha(160);
        selectShape.setShape(ShapeElement.OVAL);
        selectShape.setBounds(0,0,48,48);

        indicator.setItemElement(normalShape,selectShape);
        indicator.setItemOffset(60);
        indicator.setPageSlider(pageSlider);
    }

    private void createAndshowPage() {
        //向页面组件当中填充内容
        for (int i = 0; i < imgIds.length; i++) {
            //一个图就有一页，然后将item的布局转换成component对象
            Component component = LayoutScatter.getInstance(this)
                    .parse(ResourceTable.Layout_item_pageslider,null,false);
            Image iv = component.findComponentById(ResourceTable.Id_itempage_iv);
            Text tv = component.findComponentById(ResourceTable.Id_itempage_tv);
            iv.setPixelMap(imgIds[i]);
            tv.setText(titles[i]);
            mDatas.add(component);
        }

        provider = new ImageSliderProvider(this, mDatas);
        pageSlider.setProvider(provider);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
